package cn.terminus.service.customer.custombusinessinfo;

import cn.terminus.biz.customer.custombusinessinfo.CustomBusinessInfoManager;
import cn.terminus.client.customer.custombusinessinfo.param.*;
import cn.terminus.client.customer.custombusinessinfo.result.CustomBusinessInfoResult;
import cn.terminus.client.customer.custombusinessinfo.service.CustomBusinessInfoService;
import cn.terminus.common.customer.bean.custombusinessinfo.dto.*;
import cn.terminus.result.common.annotation.TerminusParamVilidator;
import cn.terminus.result.common.context.TerminusCallContext;
import cn.terminus.result.common.exception.DataAccessException;
import cn.terminus.result.common.exception.ServiceException;
import cn.terminus.result.common.page.TerminusPageAble;
import cn.terminus.result.common.page.TerminusPageInfo;
import cn.terminus.result.common.result.TerminusResult;
import cn.terminus.result.common.util.BeanCopyUtils;
import cn.terminus.service.customer.common.AbstractTerminusService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (CustomBusinessInfo)表服务实现类
 *
 * @author makejava
 * @since 2021-07-08 11:18:36
 */
@Service
public class CustomBusinessInfoServiceImpl extends AbstractTerminusService implements CustomBusinessInfoService {
    /**
     * 日志
     */
    protected final Logger logger = LoggerFactory.getLogger(getClass());
    @Resource
    private CustomBusinessInfoManager customBusinessInfoManager;

    /**
     * 新增
     *
     * @param param
     * @param context
     * @return
     * @throws ServiceException
     */
    @Override
    @TerminusParamVilidator(argsIndexs = {0, 1})
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public TerminusResult<String> add(CustomBusinessInfoNewParam param, TerminusCallContext context) throws ServiceException {
        CustomBusinessInfoNewDto newDto = new CustomBusinessInfoNewDto();
        newDto.setId(genUUID());
        BeanCopyUtils.copyProperties(param, context, newDto);
        // todo
        newDto.setBusinessId(context.getBusiness().getBusinessId());
        try {
            customBusinessInfoManager.add(newDto);
        } catch (DataAccessException e) {
            logger.error("新增失败,错误信息：{}", e.getMessage());
            throw new ServiceException(e);
        }
        return TerminusResult.success(String.valueOf(newDto.getId()));
    }

    /**
     * 更新
     *
     * @param param
     * @param context
     * @return
     * @throws ServiceException
     */
    @Override
    @TerminusParamVilidator(argsIndexs = {0, 1})
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public TerminusResult<Boolean> update(CustomBusinessInfoEditParam param, TerminusCallContext context) throws ServiceException {
        CustomBusinessInfoNewDto editDto = new CustomBusinessInfoNewDto();
        BeanCopyUtils.copyProperties(param, context, editDto);
        try {
            customBusinessInfoManager.update(editDto);
        } catch (DataAccessException e) {
            logger.error("更新失败,错误信息：{}", e.getMessage());
            throw new ServiceException(e);
        }
        return TerminusResult.success(true);
    }

    /**
     * 删除
     *
     * @param param
     * @param context
     * @return
     * @throws ServiceException
     */
    @Override
    @TerminusParamVilidator(argsIndexs = {0, 1})
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public TerminusResult<Boolean> delete(CustomBusinessInfoDeleteParam param, TerminusCallContext context) throws ServiceException {
        CustomBusinessInfoDeleteDto deleteDto = new CustomBusinessInfoDeleteDto();
        BeanCopyUtils.copyProperties(param, context, deleteDto);
        try {
            customBusinessInfoManager.delete(deleteDto);
        } catch (DataAccessException e) {
            logger.error("删除失败,错误信息：{}", e.getMessage());
            throw new ServiceException(e);
        }
        return TerminusResult.success(true);
    }


    /**
     * 查询
     *
     * @param param
     * @param context
     * @return
     * @throws ServiceException
     */
    @Override
    @TerminusParamVilidator(argsIndexs = {0, 1})
    public TerminusResult<List<CustomBusinessInfoResult>> query(CustomBusinessInfoQueryParam param, TerminusCallContext context) throws ServiceException {
        CustomBusinessInfoQueryDto queryDto = new CustomBusinessInfoQueryDto();
        BeanCopyUtils.copyProperties(param, queryDto);
        try {
            List<CustomBusinessInfoResultDto> queryResultList = customBusinessInfoManager.query(queryDto);
            return TerminusResult.success(BeanCopyUtils.copy(queryResultList, CustomBusinessInfoResult.class));
        } catch (DataAccessException e) {
            logger.error("查询失败,错误信息：{}", e.getMessage());
            throw new ServiceException(e);
        }

    }


    /**
     * 分页查询
     *
     * @param param
     * @param context
     * @return
     * @throws ServiceException
     */
    @Override
    @TerminusParamVilidator(argsIndexs = {0, 1})
    public TerminusResult<TerminusPageAble<CustomBusinessInfoResult>> page(CustomBusinessInfoQueryParam param, TerminusPageInfo page, TerminusCallContext context) throws ServiceException {
        CustomBusinessInfoQueryDto queryDto = new CustomBusinessInfoQueryDto();
        BeanCopyUtils.copyProperties(param, queryDto);
        try {
            TerminusPageAble<CustomBusinessInfoResultDto> TerminusPageAble = customBusinessInfoManager.page(queryDto, page);
            if (CollectionUtils.isEmpty(TerminusPageAble.getDatas())) {
                return TerminusResult.success(new TerminusPageAble<>(new ArrayList<>(), page, 0));
            }
            return TerminusResult.success(new TerminusPageAble<>(BeanCopyUtils.copy(TerminusPageAble.getDatas(), CustomBusinessInfoResult.class),
                    page, TerminusPageAble.getTotalRows()));
        } catch (DataAccessException e) {
            logger.error("分页查询失败,错误信息：{}", e.getMessage());
            throw new ServiceException(e);
        }

    }
}
